package com.lzl.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzl.common_utils.R;
import com.lzl.eduservice.entity.EduTeacher;
import com.lzl.eduservice.entity.vo.TeacherQuery;
import com.lzl.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-03-25
 */

@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询讲师表所有数据---使用rest风格
    @ApiOperation(value = "查询讲师表所有数据")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
      /*  try {
            int i =1/0;
        }catch(Exception e){
            //执行自定义异常
            throw new LZLException(20001,"执行了LZL自定义异常");
        }*/
        return R.ok().data("items",list);
    }

    //逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/delete/{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师方法
     * @param current  当前页
     * @param limit   每页显示s数量
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current,
                             @PathVariable("limit") Long limit){
        //创建Page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页  底层封装，把分页的所有数据都封装到pageTeacher对象
        eduTeacherService.page(pageTeacher,null);
        //返回的数据data需要参考文档--看格式
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
//        return R.ok().data("total",total).data("rows",records);
        //第二种方式
        Map map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    //条件查询带分页的方法
    @PostMapping ("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建Page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页
        eduTeacherService.pageQuery(pageTeacher,teacherQuery);
        //返回的数据data需要参考文档--看格式
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R saveTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save ) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据 讲师id 进行查询
    @GetMapping("getTeacher/{id}")
    @ApiOperation(value = "根据讲师id 进行查询")
    public R getTeacherById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    //讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b ) {
            return R.ok();
        }else {
            return R.error();
        }
    }


}

